{extend name="layout/base"}
{block name='content'}
<div class="layui-fluid">
    <div class="layui-form">
        <div class="layui-col-md6">
            <div class="layui-form-item">
                <label class="layui-form-label">名称：</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="name" placeholder="请输入角色名称" lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">权限</label>
                <div class="layui-input-block">
                    <div id="transfer"></div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="submit">提交</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['transfer', 'jquery', 'form'], function(transfer, $, form){
        getPurviewData();
        function getPurviewData() {
            $.ajax({
                url: "{:url('purview/lists')}",
                type: 'post',
                success: function (res) {
                    var data = [];
                    for (let i = 0; i < res.data.length; i++) {
                        data.push({
                            title: res.data[i].purview_name,
                            value: res.data[i].purview_id
                        })
                    }
                    //基础效果
                    transfer.render({
                        elem: '#transfer',
                        title: ['全部权限', '角色权限'],
                        data: data,
                        id: 'demo1'
                    });


                },
                error: function () {
                    layer.msg('请求错误', {icon: 5});
                }
            });
        };
        form.on('submit(submit)', function (res) {
            var getData = transfer.getData('demo1');
            var data = res.field;
            var purviewId = ''
            for (let i = 0; i < getData.length; i++) {
                purviewId += getData[i].value+',';
            }
            data.purviewId = purviewId
            console.log(purviewId)
            $.ajax({
                url: "{:url('role/add')}",
                data: data,
                success: function (res) {
                    if (res.code == 0) {
                        location.href = "{:url('role/lists')}"
                    } else {
                        layer.msg(res.msg)
                    }
                }
            })
            return false
        })

    });
</script>
{/block}